package cn.edu.lzu.common.util;

import com.sun.xml.internal.messaging.saaj.packaging.mime.MessagingException;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @PackageName: cn.edu.lzu.common.util
 * @ClassName: Email
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2020/5/5 8:53
 * @Version: 1.0
 */
public class Email {
    public static void sendEmail(String toAddress, String subject, String content) throws MessagingException, javax.mail.MessagingException {
        Properties properties = new Properties();
        properties.setProperty("mail.host", "smtp.qq.com");
        properties.setProperty("mail.transport.protocol", "smtp");
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.ssl.enable", "true");

        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("3098086691@qq.com", "dcbluoairguqdedh");
            }
        });
        session.setDebug(true);

        Transport transport = session.getTransport();
        transport.connect("smtp.qq.com", "3098086691@qq.com", "dcbluoairguqdedh");

        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress("3098086691@qq.com"));
        mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
        mimeMessage.setSubject(subject);
        mimeMessage.setContent(content, "text/html;charset=UTF-8");
        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
        transport.close();

    }
}
